Namespace DataStream
    Public Class StreamCalculatorConfigForm

        Private mCalculator As StreamCalculator
        Private mLog As StreamLog

        Sub New(ByVal Calculator As StreamCalculator, ByVal Log As StreamLog)
            MyBase.New()
            Me.InitializeComponent()

            mCalculator = Calculator
            mLog = Log

            Me.StreamCalculatorItemColectionBindingSource.RaiseListChangedEvents = True
            Me.StreamCalculatorItemColectionBindingSource.DataSource = mCalculator.CalculatorItemCollection

            For Each str As String In Log.ValidItems
                uxlbAvailableStreamItems.Items.Add(str)
            Next

        End Sub

        Private Sub StreamCalculatorItemBindingSource_AddingNew(ByVal sender As System.Object, ByVal e As System.ComponentModel.AddingNewEventArgs) Handles StreamCalculatorItemColectionBindingSource.AddingNew
            Dim source As BindingSource = CType(sender, BindingSource)
            Dim obj As New StreamCalculatorItem(String.Format("New{0} = 1.00", source.Count))

            e.NewObject = obj

        End Sub

        Private Sub StreamCalculatorItemColectionBindingSource_ListChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles StreamCalculatorItemColectionBindingSource.ListChanged
            Select Case e.ListChangedType
                Case ListChangedType.ItemAdded, ListChangedType.ItemChanged
                    If Not mCalculator.CalculatorItemCollection(0).EvalObject Is Nothing Then
                        ' mCalculator.CalculatorItemCollection(0).EvalObject.Draw(New Point(1, 1), Me.uxPicBox.CreateGraphics)
                        mCalculator.CalculatorItemCollection(0).EvalObject.Evaluate(New Stream())
                    End If
                Case ListChangedType.ItemDeleted
                    Debug.Print("Item deleted")
            End Select

        End Sub

        ''' <summary>
        ''' Applies the current calculation to the current stream
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub uxBtnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxBtnApply.Click
            If mLog.Count Then
                For Each stream As DataStream.Stream In mLog
                    For Each calc As StreamCalculatorItem In mCalculator.CalculatorItemCollection
                        If Not calc.EvalObject Is Nothing Then
                            calc.EvalObject.Calculate(stream)
                        End If
                    Next
                Next
            End If
        End Sub

        ''' <summary>
        ''' Removes all the current calculated data from the stream
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub uxBtnClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxBtnClearAll.Click
            For Each stream As DataStream.Stream In mLog
                For i As Integer = stream.Count - 1 To 0 Step -1
                    If stream(i).ItemType = StreamItemType.UserValue Then
                        stream.Remove(stream(i))
                    End If
                Next
            Next

            ' Cause the stream to update itself
            mLog.ValidItemsRefresh()
        End Sub

        Private Sub uxBtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxBtnExit.Click
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End Sub

        Private Sub uxGridEvals_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles uxGridEvals.DataError
            Dim grid As DataGridView = CType(sender, DataGridView)
            e.Cancel = True
        End Sub

       
        Private Sub uxGridEvals_UserDeletingRow(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles uxGridEvals.UserDeletingRow
            Debug.Print("Attemting to delte row")
        End Sub

        Private Sub uxGridEvals_UserDeletedRow(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles uxGridEvals.UserDeletedRow
            Debug.Print("Row Deleted")
        End Sub
    End Class
End Namespace

